package com.woniuxy.Controller;

import com.woniuxy.Service.ManagerService;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.entity.User;
import jakarta.validation.Valid;
import org.aspectj.bridge.Message;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author：向天浩
 * @Date：2022/1/26 12:03
 * @Description :TODO
 */
//controller就是servlet层接收请求，然后进行相应
@RestController
@RequestMapping("/managercontroller")
public class ManagerController{
    @Autowired
    ManagerService managerService;

    //PostMapping请求方式的注解
    //RequestBody参数是对象类型的时候可以在参数面前添加该注解，会自动将json格式的数据转为java对象
    //在同一个接收方法中只能有一个RequestBody注解
    //RequestBody是获取请求体中的参数，所以必须搭配POST请求来使用
    @PostMapping("/add")
    public RbacManager add(@RequestBody RbacManager rbacManager) {
        System.out.println("是否进入----------------------------------------");
        managerService.add(rbacManager);
             return rbacManager;
    };

    //PathVariable;路径上的参数，使用GET请求的时候URL上所带的参数使用这个注解可以获取，多个参数时，注解中需要写明参数的key是什么
    //比如@PathVariable("name")String name,@PathVariable("id")int id
    @GetMapping("/find/{id}")
    public RbacManager select(@PathVariable int id){

      return managerService.findById(id);
    };

    //这个RequestMapping注解中设置的参数解决了中文返回值乱码的问题produces="login/html;charset=utf-8"
    @RequestMapping(method=RequestMethod.POST,value="login",produces="login/html;charset=utf-8")
    public String findByAccount(@RequestBody RbacManager rbacManager){
        RbacManager rmr=managerService.findByAccount(rbacManager.getAccount());
        if (rmr!=null && rmr.getPassword().equals(rbacManager.getPassword())) {
            return "成功";
        }else {
            return "失败";
        }
    }

    @RequestMapping(method=RequestMethod.POST,value="register",produces="register/html;charset=utf-8")
    public String findByAccount(String account){
        RbacManager byAccount = managerService.findByAccount(account);
        if (byAccount != null) {
            return "no";
        }else {
            return "yes";
        }
    }

    public String addUser(@Validated @RequestBody User user){


    return "1";
    }


}
